package com.cie2.janji.services;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Order;

import com.cie2.janji.entities.Promise;

public class PromiseService {

	private Session session;
	
	public PromiseService(Session session) {
		this.session = session;
	}
	
	public void add(Promise promise) {
		//
		
		session.persist(promise);
		session.flush();
	}
	
	public Promise load(Long promiseId) {
		return (Promise) session.load(Promise.class, promiseId);
	}
	
	public List getAll() {
		return session.createCriteria(Promise.class)
				.addOrder(Order.desc("createdDate"))
				.addOrder(Order.desc("startDate"))
				.list();
	}
	
	public List getHeadline() {
		return session.createCriteria(Promise.class)
			.addOrder(Order.desc("createdDate"))
			.addOrder(Order.desc("startDate"))
			.setMaxResults(3) // always use in pair with fetch size for performance
			.setFetchSize(3)
			.list();
	}
	
	public Promise getLatestPromise() {
		return new Promise();
	}
}
